var prefix = "/cms/material"
var kindPrefix = "/cms/kind"
var imgId = undefined

$().ready(function() {
  validateRule();
});

$.validator.setDefaults({
  submitHandler : function() {
    save();
  }
});

layui.use('upload', function () {
  var $ = layui.jquery
    , upload = layui.upload;
  //拖拽上传
  upload.render({
    accept: 'images',    // 限制文件类型
    size: 1024, // 限制文件大小
    elem: '#cloud-upload',
    url: '/common/sysFile/upload', //改成您自己的上传接口
    before: function(obj){ // obj参数包含的信息
      layer.load(1); //上传loading
    },
    done: function (res) {
      layer.closeAll('loading'); //关闭loading
      layer.msg('图片上传成功');
      layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.file.url);
      imgId = res.file.id
    },
    error: function(res){
      layer.closeAll('loading'); //关闭loading
      layer.msg(res.msg);
    }
  });

});

function save() {
  let param = $.param({
    imgId:imgId
  });
  $.ajax({
    cache : true,
    type : "POST",
    url : prefix + "/save",
    data : param +"&"+ $('#signupForm').serialize(),// 你的formid
    async : false,
    error : function(request) {
      parent.layer.alert("Connection error");
    },
    success : function(data) {
      if (data.code == 0) {
        parent.layer.msg("操作成功");
        parent.location.reload();
        var index = parent.layer.getFrameIndex(window.name);  // 获取窗口索引
        parent.layer.close(index);
      } else {
        parent.layer.alert(data.msg)
      }

    }
  });

}
function validateRule() {
  var icon = "<i class='fa fa-times-circle'></i> ";
  $("#signupForm").validate({
    rules : {
      number : {
        digits : true,
        required : true,
        min : 1
      },
    },
    messages : {
      number : {
        required : icon + "请输入 数量",
        digits : icon + "请输入 正整数"
      }
    }
  })
}

var openKind = function(){
  layer.open({
    type:2,
    title:"选择类别",
    area : [ '300px', '450px' ],
    content: kindPrefix + "/treeView"
  })
}

function loadKind( kindId,kindName){
  $("#kindId").val(kindId);
  $("#kindName").val(kindName);
}
